constructive algorithms *1300

Please click on ads to support us..

C++ Code:

/*
     ***   **  *   *  **  *    *          *   *   **   * ***** *     *
     *    *  *  * *  *  * * *  *          ** **  *  *  *   *    *   *
     ***  ****   *   **** *  * *          * * *  ****  *   *      *
       *  *  *   *   *  * *   **          *   *  *  *  *   *      *
     ***  *  *   *   *  * *    *          *   *  *  *  *   *      *
*/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
/*********************Sayan Maity****************************/
#define ll long long
#define ull unsigned long long
#define os(x) cout<<x<<" ";
#define on(x) cout<<x<<endl;
#define ump unordered_map
#define pb push_back
#define all(x) x.begin(), x.end()
#define nn cout<<endl;
#define o(x) cout<<x;
#define in(x) cin>>x;
#define in2(x , y) cin>>x>>y;
#define in3(x , y , z) cin>>x>>y>>z;
#define in4(x , y , z , m) cin>>x>>y>>z>>m;
#define in5(x , y , z , m , n) cin>>x>>y>>z>>m>>n;
#define vt vector
#define pb push_back
/*********************Sayan Maity****************************/
#define mod 1000000007
#define FOR(i , x , n) for(int i=x ; i<=n ; ++i)
#define FORR(i , x , n) for(int i=x ; i>=n ; --i)
#define FOR1(i, n) for(int i=1; i<=n; ++i)
#define FOR0(i, n) for(int i=0; i<n; ++i)
#define con continue;
int dx[4]={-1, 0, 1, 0};
int dy[4]={0, 1, 0, -1};
//ar.erase(unique(ar.begin(), ar.end()), ar.end()); //remove duplicates from a vector
bool is_prime[1000006];
template <typename T>
void swap(T &a , T &b){
    T temp=a;
    a=b;
    b=temp;
}
template <typename T>
T bisearch(T a[] , T l , T r , T x){
    if(l<=r){
        T mid=(l+r)/2;
        if(a[mid]==x)
            return mid;
        if(a[mid]>x)
            return bisearch(a , l , mid-1 , x);
        if(a[mid]<x)
            return bisearch(a , mid+1 , r , x);
    }
    return INT_MIN;
}

template <typename T>
T pwr(T x, T y)
{
    T temp;
    if( y == 0)
        return 1;
    temp = pwr(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
template <typename T>
T gcd(T a,  T b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
 /*
 int a[] = {0, 1, 2, 3, 4, 5};
    for (int n : a)
        std::cout << n << ' ';
 */
template <typename T>
T ncr(T n, T r)
{
if(r==0|r==n)
  return 1;
else
  return ncr(n-1,r-1)+ncr(n-1,r);
}

template <typename T>
void soE(T n)

{
    memset(is_prime , true , sizeof(is_prime));
    is_prime[0]=is_prime[1]=false;
    for (int p=2; p*p<=n; p++)
    {  /*********************Sayan Maity****************************/
        if (is_prime[p] == true)
        {
             for (int i=p*p; i<=n; i += p)
                is_prime[i] = false;
        }
    }

}
template <typename T>
void print(vector<T> a)
{
	int size=a.size();
	for(int i=0; i<size; ++i)
		cout<<a[i]<<" ";
	cout<<endl;
}
/********************************debug**************************************/
template <typename T>
void debug(vector<T> a, string name)
{
	cerr<<"debug"<<" "<<name<<endl;
	for(int i=0; i<(int)a.size(); ++i)
		cerr<<a[i]<<" ";
	cerr<<endl;
}
template <typename T>
void debug(deque<T> a, string name)
{
	cerr<<"debug"<<" "<<name<<endl;
	for(int i=0; i<(int)a.size(); ++i)
		cerr<<a[i]<<" ";
	cerr<<endl;
}
template <typename T>
void debug(vector<pair<T, T> > a, string name)
{
	cerr<<"debug"<<" "<<name<<endl;
	cerr<<"first"<<endl;
	int n=a.size();
	for(int i=0; i<n; ++i)
		cerr<<a[i].first<<" ";
	cerr<<endl;
	cerr<<"second"<<endl;
	for(int i=0; i<n; ++i)
		cerr<<a[i].second<<" ";
	cerr<<endl;
	
}
template <typename T>
void debug(vector<vector<T>> a, string name)
{
	cerr<<"debug "<<name<<endl;
	int n=a.size();
	for(int i=0; i<n; ++i)
	{	
		cerr<<i<<"->";
		for(int j=0; j<(int)a[i].size(); ++j)
			cerr<<a[i][j]<<" ";
		cerr<<endl;
	}
}
template <typename T>
void debug(priority_queue<T> pq, string name)
{
	cout<<name<<endl;
	while(!pq.empty())
	{
		cout<<pq.top()<<endl;
		pq.pop();
	}
	cout<<"end "<<name<<endl;
	cout<<endl;
}
template <typename T>
void debug(priority_queue<T, vector<T>, greater<T>> pq, string name)
{
	cout<<name<<endl;
	while(!pq.empty())
	{
		cout<<pq.top()<<endl;
		pq.pop();
	}
	cout<<"end "<<name<<endl;
	cout<<endl;
}
void debug(unordered_map<string, int> mp, string name)	
{
	cerr<<"debug "<<name<<endl;
	for(auto it: mp)
	{
		cerr<<it.first<<"  "<<it.second<<endl;
	}
}
template <typename T>
void debug(unordered_map<T, T> mp, string name)	
{
	cerr<<"debug "<<name<<endl;
	for(auto it: mp)
	{
		cerr<<it.first<<"  "<<it.second<<endl;
	}
}
template <typename T>
void debug(T x, string name)
{
	cerr<<name<<":"<<x<<endl;
}
/*
__builtin_popcount(n);// return number of setbits present in binary representation in n;
auto it=upper_bound(v.begin() , v.end() , 7);//returns iterator or pointers of greater than the element 

auto it=lower_bound(v.begin() , v.end() , 7);//returns iterator or pointers of the element or greater than the element 

array or vector should be sorted

auto it=min_element(v.begin() , v.end());//returns pointer

auto it=max_element(v.begin() , v.end());//

***incase of set or map syntax: ob.upper/lower_bound(----) 

int sum=accumulate(v.begin() , v.end() , x);//return sum of the vector+x

int ct=count(v.begin() , v.end() , x);

find(v.begin() , v.end , x);

reverse(v.begin() , v.end());

lambda function

syntax: fun=[](int x){return x%2==0;}

all_of(v.begin() , v.end() , fun);
any_of(v.begin() , v.end() , fun);
none_of(v.begin() , v.end() , fun);
this 3 functions return true or false;
*/

/*
bool cmp(vec<pair<int , int> > &a , vec<pair<int , int> > &b){
    return a.second<b.second;
}
sort(vp.begin() , vp.end() , cmp);
*/

class dsu
{
	public:
	vector<int> par, rank;
	dsu()
	{
		
	}
	dsu(int size)
	{
		for(int i=0 ; i<=size ; ++i)
		{
			par.push_back(i);
			rank.push_back(1);
		}
	}
	int find(int x)
	{
		if(par[x]==x)
			return x;
		int temp=find(par[x]);
		par[x]=temp;
		return temp;
	}
	
	void makeUnion(int x, int y)
	{
		int lx=find(x), ly=find(y);
		if(lx!=ly)
		{	
			if(rank[lx]>rank[ly])
			{
				par[ly]=lx;
			}
			else if(rank[lx]<rank[ly])
			{
				par[lx]=ly;
			}else{
				par[lx]=ly;
				rank[ly]++;
			}
		}
	}
	void display()
	{
		for(int i=0; i<(int)par.size(); ++i)
			cerr<<i<<" ";
		cerr<<endl;
		for(int i=0; i<(int)par.size(); ++i)
		cerr<<par[i]<<" ";
		cerr<<endl;
	}
};
bool cmp(pair<int , int>  &a , pair<int , int>  &b){
    return a.second<b.second;
}
//vector<vector<int> > v(n , vector<int>(n , 0))
/*********************Sayan Maity****************************/
int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    std::ofstream error("error.txt");
    std::streambuf *errbuf = std::cerr.rdbuf(error.rdbuf());
                         
	//	cerr<<"case "<<T<<endl;       
	int a, b, n;
	cin>>a>>b>>n;
	deque<int> ans;
	bool f=true;
	for(int i=0; i<(int)ceil(n/2.0); ++i)
	{
		ans.push_back(f);
		f=!f;
		ans.push_back(f);
		f=!f;
		a--;
		b--;
	}
	if(n%2==0)
	{
		if(a)
		{
			while(b--)
				ans.push_front(1);
			while(a--)
				ans.push_front(0);
		}else if(b)
		{
			while(a--)
				ans.push_back(0);
			while(b--)
				ans.push_back(1);
		}
	}else
	{
		while(a--)
			ans.push_back(0);
		while(b--)
			ans.push_front(1);
	}
	/*while(a>1)
		ans.push_back(0), a--;
	while(b>1)
		ans.push_front(1), --b;
	if(n%2==0)
	{
		
		if(a)
			ans.push_front(0), --a;
		else if(b)
			ans.push_back(1), --b;
	}*/
	/*	while(a--)
			ans.push_back(0);
		while(b--)
			ans.push_front(1);*/
	
/*	if(n%2==0)
	{
		int cnta=n/2, cntb=n/2;
		int f=1;
		while(cnta or cntb)
		{
			if(f and cntb)
			{
				ans.push_back(f);
				f=0;
				cntb--;
			}else if(cnta)
			{
				ans.push_back(f);
				f=1;
				cnta--;
			}
		}
		debug(ans, "ans");
		cnta=a-n/2, cntb=b-n/2;
		debug(cnta, "cnta");
		debug(cntb, "cntb");
		if(cnta)
			ans.push_front(0), cnta--;
		else if(cntb)
			ans.push_back(1), cntb--;
		debug(ans, "ans");
		debug(cnta, "cnta");
		while(cnta--)
			ans.push_back(0);
		
		while(cntb--)
			ans.push_front(1);
		debug(ans, "ans");
			
	}else{
		int cnta=n/2+1, cntb=n/2+1;
		int f=1;
		while(cnta or cntb)
		{
			if(f and cntb)
			{
				ans.push_back(f);
				f=0;
				cntb--;
			}else if(cnta)
			{
				ans.push_back(f);
				f=1;
				cnta--;
			}
		}
		debug(cnta, "cnta");
		debug(cntb, "cntb");
		cnta=a-(n/2+1), cntb=b-(n/2+1);
		debug(ans, "ans");
		while(cnta--)
			ans.push_back(0);
		while(cntb--)
			ans.push_front(1);
		
	}*/
	for(int i=0; i<(int)ans.size(); ++i)
		cout<<ans[i];
	
	/*
	1.check corner case.
	*/
    
    std::cerr.rdbuf(errbuf);
    return 0;

}


Comments

Submit
0 Comments
More Questions

137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence